﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.Logic
{
    //名称  测试模组{_testerIndex + 1}1,2,3
    public class TesterLogic : FlowBase
    {
        //0,1
        private int _testerIndex;
        //操作面侧 0模组  对面 1模组，分别对应两个测试工控机
        private int _moduleIndex;
        private CommunicationHelper _testerClient;
        private bool[] _resultReceivedFlag;
        private bool[] _result;
        private bool[] _enableReceivedFlag;
        private bool[] _enable;
        private bool[] _readyReceivedFlag;
        private bool[] _ready;
        private bool[] _vacReceivedFlag;
        private bool[] _vac;
        private bool[] _startTestReceivedFlag;
        private bool[] _startTest;
        public TesterStatus CurrentTesterStatus { get; set; }
        public Tray TesterTray { get; set; }
        public bool[] BankEnableStatus { get; set; }
        public static bool IsInit = false;
        public TesterLogic(string flowName, FlowManager flowmanager, int moduleIndex, int testerIndex)
            : base(flowName, flowmanager)
        {
            this._moduleIndex = moduleIndex;
            this._testerIndex = testerIndex;

            //_testerClient = new CommunicationHelper($"测试工控机治具{_moduleIndex}{_testerIndex}", MachineResource.Instance.MachineTcpClient[$"测试工控机治具{_moduleIndex}{_testerIndex}"]);
            //_testerClient.NotifyMessageReceived += testerClient_NotifyMessageReceived;

            this.TesterTray = new Tray(1, 2);
            this._resultReceivedFlag = new bool[2] {false, false };
            this._result = new bool[2] {false, false };
            this._enableReceivedFlag = new bool[2] { false, false };
            this._enable = new bool[2] { false, false };
            this._readyReceivedFlag = new bool[2] { false, false };
            this._ready = new bool[2] { false, false };
            this._vacReceivedFlag = new bool[2] { false, false };
            this._vac = new bool[2] { false, false };
            this._startTestReceivedFlag = new bool[2] { false, false };
            this._startTest = new bool[2] { false, false };
            this.BankEnableStatus = new bool[2] { true, true };
        }

        string strTemp = "";
        private void testerClient_NotifyMessageReceived(ReceivePackage obj)
        {
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"收到数据{obj.Message}", "", 0, "系统"));
            strTemp += obj.Message;
            while (strTemp.Contains("End"))
            {
                int index = strTemp.IndexOf("End");
                string messageString = strTemp.Substring(0, index + 3);
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"派发信息{messageString}", "", 0, "系统"));
                //for (int i = 0; i < FrontTesters.Count; i++)
                {
                    messageDispatch(messageString);
                }
                strTemp = strTemp.Substring(index + 3);
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"处理后信息{strTemp}", "", 0, "系统"));

            }
        }

        public void messageDispatch(string messageString)
        {
            string[] res;
            if (messageString.Contains($"Enable,{_testerIndex}"))
            {
                res = messageString.Split(',');
                int testerId = Convert.ToInt32(res[1]);
                int bankNo = Convert.ToInt32(res[2]);
                bool isOK = res[3].ToUpper().Equals("OK");
                _enableReceivedFlag[bankNo] = true;
                _enable[bankNo] = isOK;
            }
            else if (messageString.Contains($"Ready,{_testerIndex}"))
            {
                res = messageString.Split(',');
                int testerId = Convert.ToInt32(res[1]);
                int bankNo = Convert.ToInt32(res[2]);
                bool isOK = res[3].ToUpper().Equals("OK");
                _readyReceivedFlag[bankNo] = true;
                _ready[bankNo] = isOK;
            }
            else if (messageString.Contains($"Load,{_testerIndex}"))
            {
                res = messageString.Split(',');
                int testerId = Convert.ToInt32(res[1]);
                int bankNo = Convert.ToInt32(res[2]);
                bool isOK = res[3].ToUpper().Equals("OK");
                _vacReceivedFlag[bankNo] = true;
                _vac[bankNo] = isOK;
            }
            else if (messageString.Contains($"StartTest,{_testerIndex}"))
            {
                res = messageString.Split(',');
                int testerId = Convert.ToInt32(res[1]);
                int bankNo = Convert.ToInt32(res[2]);
                bool isOK = res[5].ToUpper().Equals("OK");
                _startTestReceivedFlag[bankNo] = true;
                _startTest[bankNo] = isOK;
            }
            //过滤结果信息
            else if (messageString.Contains($"TestResult,{_testerIndex}"))
            {
                res = messageString.Split(',');
                int testerId = Convert.ToInt32(res[1]);
                int bankNo = Convert.ToInt32(res[2]);
                string sn = res[3];
                bool isOK = res[4].ToUpper().Equals("OK");
                string ngBin = res[5];
                //if(TesterTray.Helper.GetCell(bankNo).SN != sn)
                //{
                //    MessageBox.Show("SN异常!");
                //    LogHelper.Instance.Log("系统", "SN异常!", MessageLevel.Log);
                //}
                _resultReceivedFlag[bankNo] = true;
                _result[bankNo] = isOK;

                if (GlobalVariable.IsGrrMode || GlobalVariable.IsCosmeticMode)
                {
                    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前GRR模式，强制OK", this.Description, 0, ""));
                    TesterTray.Helper.SetCellResult(bankNo, TestResult.FirstTimeOK);
                    lock (GlobalVariable.SNLock)
                    {
                        if (!GlobalVariable.SN2TestCount.ContainsKey(TesterTray.Helper.GetCell(bankNo).SN))
                        {
                            GlobalVariable.SN2TestCount.Add(TesterTray.Helper.GetCell(bankNo).SN, new int[3] { 0, 0, 0 });
                            GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(bankNo).SN][_testerIndex % 3]++;
                        }
                        else
                        {
                            GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(bankNo).SN][_testerIndex % 3]++;
                        }
                    }
                }
                else
                {
                    if (SystemConfig.Instance.UsingStatus.IsEnforceOK)
                    {
                        if (TesterTray.Helper.GetCell(bankNo).Result == TestResult.FirstTimeNG)
                            TesterTray.Helper.SetCellResult(bankNo, TestResult.SecondTimeOK);
                        else if (TesterTray.Helper.GetCell(bankNo).Result == TestResult.SecondTimeNG)
                            TesterTray.Helper.SetCellResult(bankNo, TestResult.ThirdTimeOK);
                        else
                            TesterTray.Helper.SetCellResult(bankNo, TestResult.FirstTimeOK);
                    }
                    else
                    {
                        if (isOK)
                        {
                            if (TesterTray.Helper.GetCell(bankNo).Result == TestResult.FirstTimeNG)
                                TesterTray.Helper.SetCellResult(bankNo, TestResult.SecondTimeOK);
                            else if (TesterTray.Helper.GetCell(bankNo).Result == TestResult.SecondTimeNG)
                                TesterTray.Helper.SetCellResult(bankNo, TestResult.ThirdTimeOK);
                            else
                                TesterTray.Helper.SetCellResult(bankNo, TestResult.FirstTimeOK);
                        }
                        else
                        {
                            if (TesterTray.Helper.GetCell(bankNo).Result == TestResult.FirstTimeNG)
                                TesterTray.Helper.SetCellResult(bankNo, TestResult.SecondTimeNG);
                            else if (TesterTray.Helper.GetCell(bankNo).Result == TestResult.SecondTimeNG)
                                TesterTray.Helper.SetCellResult(bankNo, TestResult.ThirdTimeNG);
                            else
                                TesterTray.Helper.SetCellResult(bankNo, TestResult.FirstTimeNG);
                        }
                    }
                }
                //增加该产品的测试履历
                TesterTray.Helper.GetCell(bankNo).TestedTesters.Add(_moduleIndex * 6 + _testerIndex);
            }
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }
        public override void InSingleRunMode()
        {
            if (GlobalVariable.OnSingleRunPause != null)
                GlobalVariable.OnSingleRunPause();
        }
        public override void Home()
        {
            if (!CheckFlowCondition()) return;

            _testerClient.EmptySendBuffer();
            if (!GlobalVariable.IsDryRun)
            {
                UpdateEnableStatus();
                if (BankEnableStatus.ToList().Any(sts => sts))
                    CurrentTesterStatus = TesterStatus.Idle;
                else
                    CurrentTesterStatus = TesterStatus.Disable;
            }
            else
                CurrentTesterStatus = TesterStatus.Idle;
            this._resultReceivedFlag = new bool[2] { false, false };

            this.TesterTray.Helper.NewEmptyTray();

            strTemp = "";
            this._resultReceivedFlag = new bool[2] { false, false };
            this._result = new bool[2] { false, false };
            this._enableReceivedFlag = new bool[2] { false, false };
            this._enable = new bool[2] { false, false };
            this._readyReceivedFlag = new bool[2] { false, false };
            this._ready = new bool[2] { false, false };
            this._vacReceivedFlag = new bool[2] { false, false };
            this._vac = new bool[2] { false, false };
            this._startTestReceivedFlag = new bool[2] { false, false };
            this._startTest = new bool[2] { false, false };
            TesterTray.Helper.SetCellBarcode(0,string.Empty);
            TesterTray.Helper.SetCellBarcode(1,string.Empty);
            isFromRetest = false;
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }
        DateTime ctStartCountTime;
        DateTime ctEndCountTime;
        public override void Method()
        {
            if (!CheckFlowCondition()) return;

            switch(CurrentTesterStatus)
            {
                case TesterStatus.Idle:

                    WaitForSignal("治具上料模组", $"测试治具{_testerIndex + 1}开始测试");
                    if (!CheckFlowCondition()) return;

                    LogHelper.Instance.Log(this.Description, $"{this.Description} 状态刷新为Testing...", MessageLevel.Log);
                    CurrentTesterStatus = TesterStatus.Testing;

                    ctStartCountTime = DateTime.Now;
                retest:
                    if (!GlobalVariable.IsDryRun && !SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                    {
                        LogHelper.Instance.Log(this.Description, $"{this.Description} 开始发送 StartTest", MessageLevel.Log);
                        if (isFromRetest)
                        {
                            isFromRetest = false;
                            if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                OpenVac(_testerIndex, 0, true);
                            }
                            if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                OpenVac(_testerIndex, 1, true);
                            }
                        }
                        SendPackage sp1 = null;
                        SendPackage sp2 = null;
                        if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(0).IsPreNG)
                        {
                            SendPackage sendPackage = new SendPackage();
                            sendPackage.Message = $"StartTest,{_testerIndex},{0},{TesterTray.Helper.GetCell(0).SN},{0},{GlobalVariable.MesConfig}";
                            StartTest(_testerIndex, 0, TesterTray.Helper.GetCell(0).SN, 0, out sp1);
                        }
                        if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(1).IsPreNG)
                        {
                            SendPackage sendPackage = new SendPackage();
                            sendPackage.Message = $"StartTest,{_testerIndex},{1},{TesterTray.Helper.GetCell(1).SN},{0},{GlobalVariable.MesConfig}";
                            StartTest(_testerIndex, 1, TesterTray.Helper.GetCell(1).SN, 0, out sp2);
                        }
                        if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(0).IsPreNG)
                        {
                            if(!WaitStartTest(_testerIndex, 0, sp1))
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                            _resultReceivedFlag[0] = false;
                        }
                        if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && !TesterTray.Helper.GetCell(1).IsPreNG)
                        {
                            if(!WaitStartTest(_testerIndex, 1, sp2))
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                            _resultReceivedFlag[1] = false;
                        }
                    }

                    break;

                case TesterStatus.Testing:

                    LogHelper.Instance.Log(this.Description, $"{this.Description} 等待测试完成", MessageLevel.Log);
                    DateTime startTime = DateTime.Now;
                    TimeSpan ts = DateTime.Now - startTime;
                    while (!AllEnabledTesterFinished() && ts.TotalMilliseconds < 120000 && !IsBreak())
                    {
                        ts = DateTime.Now - startTime;
                        Thread.Sleep(10);
                    }
                    if (!CheckFlowCondition()) return;
                    if (!AllEnabledTesterFinished())
                    {
                        if (ts.TotalMilliseconds >= 120000)
                        {
                            ShowWarnAndPause($"等待测试测试模组{_testerIndex + 1}测试完成超时!测试完成状态:{string.Join(",", _resultReceivedFlag)}", "系统");
                            if (MessageBox.Show("是否重发测试指令?", "测试", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                        }
                    }
                    if(GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                    {
                        if (GlobalVariable.IsGrrMode || GlobalVariable.IsCosmeticMode)
                        {
                            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前GRR模式，强制OK", this.Description, 0, ""));
                            if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);
                                lock (GlobalVariable.SNLock)
                                {
                                    if (!GlobalVariable.SN2TestCount.ContainsKey(TesterTray.Helper.GetCell(0).SN))
                                    {
                                        GlobalVariable.SN2TestCount.Add(TesterTray.Helper.GetCell(0).SN, new int[3] { 0, 0, 0 });
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(0).SN][_testerIndex % 3]++;
                                    }
                                    else
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(0).SN][_testerIndex % 3]++;
                                }
                            }

                            if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);
                                lock (GlobalVariable.SNLock)
                                {
                                    if (!GlobalVariable.SN2TestCount.ContainsKey(TesterTray.Helper.GetCell(1).SN))
                                    {
                                        GlobalVariable.SN2TestCount.Add(TesterTray.Helper.GetCell(1).SN, new int[3] { 0, 0, 0 });
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(1).SN][_testerIndex % 3]++;
                                    }
                                    else
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(1).SN][_testerIndex % 3]++;
                                }
                            }
                        }
                        else
                        {
                            if (SystemConfig.Instance.UsingStatus.IsEnforceOK)
                            {
                                if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                //{
                                //    lock (GlobalVariable.TesterOKDataLock)
                                //    {
                                //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                //    }
                                //}
                                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                //{
                                //    lock (GlobalVariable.TesterOKDataLock)
                                //    {
                                //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                //    }
                                //}
                                //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));

                            }
                            else
                            {
                                Random rd = new Random();
                                int val = rd.Next(1, 10);
                                if (val <= 1)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeNG);
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                    }
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }
                                }
                                else if (val >= 2 && val <= 7)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //}
                                    //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //}
                                    //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                }
                                else if (val == 8)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                    //}
                                }
                                else if (val == 9)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                    //}

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeNG);
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                    }
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                                        //if (!GlobalVariable.IsOnlineRetest)
                                        //{
                                        //    lock (GlobalVariable.TesterNGDataLock)
                                        //    {
                                        //        GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //    }
                                        //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        //}
                                    }
                                }
                                else
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                        //if (!GlobalVariable.IsOnlineRetest)
                                        //{
                                        //    lock (GlobalVariable.TesterNGDataLock)
                                        //    {
                                        //        GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //    }
                                        //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        //}
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                    //}
                                }
                            }
                        }
                        //增加该产品的测试履历
                        TesterTray.Helper.GetCell(0).TestedTesters.Add(_moduleIndex * 6 + _testerIndex);
                        TesterTray.Helper.GetCell(1).TestedTesters.Add(_moduleIndex * 6 + _testerIndex);
                    }
                    this._resultReceivedFlag = new bool[2] { false, false };
                    CurrentTesterStatus = TesterStatus.TestFinish;
                    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"{this.Description}测试完成", this.Description, 0, "系统"));
                    ctEndCountTime = DateTime.Now;
                    LogHelper.Instance.Log(this.Description, $"测试时间:{(ctEndCountTime - ctStartCountTime).TotalMilliseconds}", MessageLevel.Log);

                    break;

                case TesterStatus.TestFinish:

                    if (_moduleIndex == 0 && (_testerIndex < 3))
                        WaitForSignal("Arm1模组", $"取测试模组1{_testerIndex + 1}完成");
                    else if (_moduleIndex == 0 && (_testerIndex >= 3))
                        WaitForSignal("Arm2模组", $"取测试模组2{_testerIndex + 1 - 3}完成");
                    else if (_moduleIndex == 1 && (_testerIndex < 3))
                        WaitForSignal("Arm3模组", $"取测试模组3{_testerIndex + 1}完成");
                    else if (_moduleIndex == 1 && (_testerIndex >= 3))
                        WaitForSignal("Arm4模组", $"取测试模组4{_testerIndex + 1 - 3}完成");
                    if (!CheckFlowCondition()) return;
                    //取完料以后，刷新一边治具的使能状态
                    //UpdateEnableStatus();
                    //如果4个穴位都不启用
                    //则不能刷新治具状态为idle
                    if (BankEnableStatus.ToList().Any(sts => sts) || GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                        CurrentTesterStatus = TesterStatus.Idle;
                    else
                        CurrentTesterStatus = TesterStatus.Disable;

                    break;

                case TesterStatus.Disable:

                    //处于禁用状态下，每隔一段时间刷新tester启用状态来看一下

                    break;
            }
        }
        bool isFromRetest = false;
        public override void SpotCheck()
        {
            if (!CheckFlowCondition()) return;

            switch (CurrentTesterStatus)
            {
                case TesterStatus.Idle:

                    if (_moduleIndex == 0 && (_testerIndex < 3))
                        WaitForSignal("Arm1模组", $"测试治具1{_testerIndex + 1}开始测试");
                    else if (_moduleIndex == 0 && (_testerIndex >= 3))
                        WaitForSignal("Arm2模组", $"测试治具2{_testerIndex + 1 - 3}开始测试");
                    else if (_moduleIndex == 1 && (_testerIndex < 3))
                        WaitForSignal("Arm3模组", $"测试治具3{_testerIndex + 1}开始测试");
                    else if (_moduleIndex == 1 && (_testerIndex >= 3))
                        WaitForSignal("Arm4模组", $"测试治具4{_testerIndex + 1 - 3}开始测试");
                    if (!CheckFlowCondition()) return;

                    CurrentTesterStatus = TesterStatus.Testing;

                    ctStartCountTime = DateTime.Now;
                retest:
                    if (!GlobalVariable.IsDryRun && !SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                    {
                        if (isFromRetest)
                        {
                            isFromRetest = false;
                            if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                OpenVac(_testerIndex, 0, true);
                            }
                            if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                OpenVac(_testerIndex, 1, true);
                            }
                        }
                        SendPackage sp1 = null;
                        SendPackage sp2 = null;
                        if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                        {
                            SendPackage sendPackage = new SendPackage();
                            sendPackage.Message = $"StartTest,{_testerIndex},{0},{TesterTray.Helper.GetCell(0).SN},{0},{GlobalVariable.MesConfig}";
                            StartTest(_testerIndex, 0, TesterTray.Helper.GetCell(0).SN, 0, out sp1);
                        }
                        if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                        {
                            SendPackage sendPackage = new SendPackage();
                            sendPackage.Message = $"StartTest,{_testerIndex},{1},{TesterTray.Helper.GetCell(1).SN},{0},{GlobalVariable.MesConfig}";
                            StartTest(_testerIndex, 1, TesterTray.Helper.GetCell(1).SN, 0, out sp2);
                        }
                        if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                        {
                            if(!WaitStartTest(_testerIndex, 0, sp1))
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                        }
                        if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                        {
                            if (!WaitStartTest(_testerIndex, 1, sp2))
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                        }
                    }

                    break;

                case TesterStatus.Testing:

                    DateTime startTime = DateTime.Now;
                    TimeSpan ts = DateTime.Now - startTime;
                    while (!AllEnabledTesterFinished() && ts.TotalMilliseconds < 2000000 && !IsBreak())
                    {
                        ts = DateTime.Now - startTime;
                        Thread.Sleep(10);
                    }
                    if (!CheckFlowCondition()) return;
                    if (!AllEnabledTesterFinished())
                    {
                        if (ts.TotalMilliseconds >= 2000000)
                        {
                            ShowWarnAndPause($"等待测试测试模组{_testerIndex + 1}测试完成超时!测试完成状态:{string.Join(",", _resultReceivedFlag)}", "系统");
                            if (MessageBox.Show("是否重发测试指令?", "测试", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                isFromRetest = true;
                                goto retest;
                            }
                        }
                    }
                    if (GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                    {
                        if (GlobalVariable.IsGrrMode || GlobalVariable.IsCosmeticMode)
                        {
                            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前GRR模式，强制OK", this.Description, 0, ""));
                            if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);
                                lock (GlobalVariable.SNLock)
                                {
                                    if (!GlobalVariable.SN2TestCount.ContainsKey(TesterTray.Helper.GetCell(0).SN))
                                    {
                                        GlobalVariable.SN2TestCount.Add(TesterTray.Helper.GetCell(0).SN, new int[3] { 0, 0, 0 });
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(0).SN][_testerIndex % 3]++;
                                    }
                                    else
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(0).SN][_testerIndex % 3]++;
                                }
                            }

                            if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);
                                lock (GlobalVariable.SNLock)
                                {
                                    if (!GlobalVariable.SN2TestCount.ContainsKey(TesterTray.Helper.GetCell(1).SN))
                                    {
                                        GlobalVariable.SN2TestCount.Add(TesterTray.Helper.GetCell(1).SN, new int[3] { 0, 0, 0 });
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(1).SN][_testerIndex % 3]++;
                                    }
                                    else
                                        GlobalVariable.SN2TestCount[TesterTray.Helper.GetCell(1).SN][_testerIndex % 3]++;
                                }
                            }
                        }
                        else
                        {
                            if (SystemConfig.Instance.UsingStatus.IsEnforceOK)
                            {
                                if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                    TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                //{
                                //    lock (GlobalVariable.TesterOKDataLock)
                                //    {
                                //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                //    }
                                //}
                                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                //{
                                //    lock (GlobalVariable.TesterOKDataLock)
                                //    {
                                //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                //    }
                                //}
                                //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));

                            }
                            else
                            {
                                Random rd = new Random();
                                int val = rd.Next(1, 10);
                                if (val <= 1)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeNG);
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                    }
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }
                                }
                                else if (val >= 2 && val <= 7)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //}
                                    //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //}
                                    //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                }
                                else if (val == 8)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                    //}
                                }
                                else if (val == 9)
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                    //}

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeNG);
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                    }
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }
                                }
                                else
                                {
                                    if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(0, TestResult.SecondTimeNG);
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(0).Result == TestResult.SecondTimeNG)
                                    {
                                        //lock (GlobalVariable.TesterNGDataLock)
                                        //{
                                        //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                        //}
                                        //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                        //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        TesterTray.Helper.SetCellResult(0, TestResult.ThirdTimeNG);
                                    }
                                    else if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                                    {
                                        TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                                        if (!GlobalVariable.IsOnlineRetest)
                                        {
                                            //lock (GlobalVariable.TesterNGDataLock)
                                            //{
                                            //    GlobalVariable.CurrentMachineData.TotalNGCount += 1;
                                            //}
                                            //GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                            //MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalNGCount.ToString(), this.Description, 0, "NG"));
                                        }
                                    }

                                    if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.FirstTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.SecondTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct && TesterTray.Helper.GetCell(1).Result == TestResult.SecondTimeNG)
                                        TesterTray.Helper.SetCellResult(1, TestResult.ThirdTimeOK);
                                    else if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                        TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeOK);

                                    //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                                    //{
                                    //    lock (GlobalVariable.TesterOKDataLock)
                                    //    {
                                    //        GlobalVariable.CurrentMachineData.TotalOKCount += 1;
                                    //    }
                                    //    GlobalVariable.SaveMachineData(GlobalVariable.CurrentMachineData);
                                    //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalOKCount.ToString(), this.Description, 0, "OK"));
                                    //}
                                }
                            }
                        }
                        //增加该产品的测试履历
                        TesterTray.Helper.GetCell(0).TestedTesters.Add(_moduleIndex * 6 + _testerIndex);
                        TesterTray.Helper.GetCell(1).TestedTesters.Add(_moduleIndex * 6 + _testerIndex);
                    }
                    this._resultReceivedFlag = new bool[2] { false, false };
                    CurrentTesterStatus = TesterStatus.TestFinish;

                    ctEndCountTime = DateTime.Now;
                    LogHelper.Instance.Log(this.Description, $"测试时间:{(ctEndCountTime - ctStartCountTime).TotalMilliseconds}", MessageLevel.CT);

                    break;

                case TesterStatus.TestFinish:

                    if (_moduleIndex == 0 && (_testerIndex < 3))
                        WaitForSignal("Arm1模组", $"取测试模组1{_testerIndex + 1}完成");
                    else if (_moduleIndex == 0 && (_testerIndex >= 3))
                        WaitForSignal("Arm2模组", $"取测试模组2{_testerIndex + 1 - 3}完成");
                    else if (_moduleIndex == 1 && (_testerIndex < 3))
                        WaitForSignal("Arm3模组", $"取测试模组3{_testerIndex + 1}完成");
                    else if (_moduleIndex == 1 && (_testerIndex >= 3))
                        WaitForSignal("Arm4模组", $"取测试模组4{_testerIndex + 1 - 3}完成");
                    if (!CheckFlowCondition()) return;
                    //取完料以后，刷新一边治具的使能状态
                    //UpdateEnableStatus();
                    //如果4个穴位都不启用
                    //则不能刷新治具状态为idle
                    if (BankEnableStatus.ToList().Any(sts => sts) || GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
                        CurrentTesterStatus = TesterStatus.Idle;
                    else
                        CurrentTesterStatus = TesterStatus.Disable;

                    break;

                case TesterStatus.Disable:

                    //处于禁用状态下，每隔一段时间刷新tester启用状态来看一下

                    break;
            }
        }
        public override void OnStop()
        {
            base.OnStop();
        }
        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }
        public void UpdateEnableStatus()
        {
            if (!isSimulate)
            {
                recheck:
                BankEnableStatus[0] = IsTesterEnabled(_testerIndex, 0);
                if(SystemConfig.Instance.UsingStatus.TesterInUseStatus[_moduleIndex * 6 + _testerIndex] && !BankEnableStatus[0])
                {
                    ShowWarnAndPause($"治具{_moduleIndex * 6 + _testerIndex}已启用但无法使能!请检查测试软件设置!","系统");
                    if (!CheckFlowCondition()) return;
                    goto recheck;
                }
                recheck2:
                BankEnableStatus[1] = IsTesterEnabled(_testerIndex, 1);
                if (SystemConfig.Instance.UsingStatus.TesterInUseStatus[_moduleIndex * 6 + _testerIndex] && !BankEnableStatus[1])
                {
                    ShowWarnAndPause($"治具{_moduleIndex * 6 + _testerIndex}已启用但无法使能!请检查测试软件设置!", "系统");
                    if (!CheckFlowCondition()) return;
                    goto recheck2;
                }
            }
        }
        public bool IsTesterEnabled(int testerId, int bankNo)
        {
            if (!GlobalVariable.IsDryRun && 
                !SystemConfig.Instance.UsingStatus.IsTesterSimulate)
            {
                SendPackage sp = new SendPackage();
                sp.Message = $"Enable,{testerId},{bankNo}\r\n";
                sp.PairRule = s =>
                {
                    return s.Contains($"Enable,{testerId},{bankNo}");
                };
                _testerClient.EmptySendBuffer();
                //TcpClientSendAndReceive(_testerClient,
                //                        sp,
                //                        5000,
                //                        0,
                //                        out string enableReply);
                TcpClientSend(_testerClient, sp);
                //if (string.IsNullOrEmpty(enableReply))
                //    return false;
                //if (enableReply.Split(',')[3].ToUpper().Equals("OK"))
                //    return true;
                DateTime startTime = DateTime.Now;
                TimeSpan ts = DateTime.Now - startTime; 
                while(!AllTesterEnabledStatusGot(bankNo) && !IsBreak() && ts.TotalMilliseconds < 5000)
                {
                    ts = DateTime.Now - startTime;
                    Thread.Sleep(20);
                }
                if (ts.TotalMilliseconds >= 5000)
                    return false;
                else
                {
                    ResetEnable(bankNo);
                    return _enable[bankNo];
                }
            }
            else
                return true;
        }
        public bool IsTesterReady(int testerId, int bankNo)
        {
            if (!GlobalVariable.IsDryRun)
            {
                SendPackage sp = new SendPackage();
                sp.Message = $"Ready,{testerId},{bankNo}\r\n";
                sp.PairRule = s =>
                {
                    return s.Contains($"Ready,{testerId},{bankNo}");
                };
                //TcpClientSendAndReceive(_testerClient,
                //                        sp,
                //                        5000,
                //                        0,
                //                        out string enableReply);
                TcpClientSend(_testerClient, sp);
                //if (string.IsNullOrEmpty(enableReply))
                //    return false;
                //if (enableReply.Split(',')[3].ToUpper().Equals("OK"))
                //    return true;
                //return false;
                DateTime startTime = DateTime.Now;
                TimeSpan ts = DateTime.Now - startTime;
                while (!AllTesterReadyStatusGot(bankNo) && !IsBreak() && ts.TotalMilliseconds < 5000)
                {
                    ts = DateTime.Now - startTime;
                    Thread.Sleep(20);
                }
                if (ts.TotalMilliseconds >= 5000)
                    return false;
                else
                {
                    ResetReady(bankNo);
                    return _ready[bankNo];
                }
            }
            else
                return true;
        }
        public bool OpenVac(int testerId, int bankNo, bool isOpen = true)
        {
            if (!GlobalVariable.IsDryRun)
            {
                SendPackage sp = new SendPackage();
                if(isOpen)
                    sp.Message = $"Load,{testerId},{bankNo},0,0\r\n";
                else
                    sp.Message = $"Load,{testerId},{bankNo},1,0\r\n";
                sp.PairRule = s =>
                {
                    return s.Contains($"Load,{testerId},{bankNo}");
                };
                //TcpClientSendAndReceive(_testerClient,
                //                        sp,
                //                        5000,
                //                        0,
                //                        out string enableReply);
                TcpClientSend( _testerClient, sp);
                DateTime startTime = DateTime.Now;
                TimeSpan ts = DateTime.Now - startTime;
                while (!AllTesterVacStatusGot(bankNo) && !IsBreak() && ts.TotalMilliseconds < 5000)
                {
                    ts = DateTime.Now - startTime;
                    Thread.Sleep(20);
                }
                if (ts.TotalMilliseconds >= 5000)
                    return false;
                else
                {
                    ResetVac(bankNo);
                    return _vac[bankNo];
                }
                //if (string.IsNullOrEmpty(enableReply))
                //    return false;
                //if (enableReply.Split(',')[3].ToUpper().Equals("OK"))
                //    return true;
                //return false;
            }
            else
                return true;
        }
        public void OpenVacNoWait(int testerId, int bankNo, bool isOpen, bool isWaitSense = true)
        {
            if (!GlobalVariable.IsDryRun)
            {
                SendPackage sp = new SendPackage();
                if (isOpen)
                {
                    if(isWaitSense)
                        sp.Message = $"Load,{testerId},{bankNo},0,0\r\n";
                    else
                        sp.Message = $"Load,{testerId},{bankNo},0,1\r\n";
                }
                else
                    sp.Message = $"Load,{testerId},{bankNo},1,0\r\n";
                sp.PairRule = s =>
                {
                    return s.Contains($"Load,{testerId},{bankNo}");
                };
                TcpClientSend(_testerClient, sp);
            }
        }
        public bool WaitVacOpen(int testerId, int bankNo)
        {
            DateTime startTime = DateTime.Now;
            TimeSpan ts = DateTime.Now - startTime;
            while (!AllTesterVacStatusGot(bankNo) && !IsBreak() && ts.TotalMilliseconds < 5000)
            {
                ts = DateTime.Now - startTime;
                Thread.Sleep(20);
            }
            if (ts.TotalMilliseconds >= 5000)
                return false;
            else
            {
                ResetVac(bankNo);
                return _vac[bankNo];
            }
        }
        public void StartTest(int testerId, int bankNo, string SN, int spotCheck, out SendPackage sp)
        {
            sp = new SendPackage();
            sp.Message = $"StartTest,{testerId},{bankNo},{SN},{spotCheck},{GlobalVariable.MesConfig}\r\n";
            sp.PairRule = s =>
            {
                return s.Contains($"StartTest,{testerId},{bankNo}");
            };
            TcpClientSend(_testerClient, sp);
        }
        public bool WaitStartTest(int testerId, int bankNo, SendPackage sp)
        {
            DateTime startTime = DateTime.Now;
            TimeSpan ts = DateTime.Now - startTime;
            while (!AllTesterStartStatusGot(bankNo) && !IsBreak() && ts.TotalMilliseconds < 10000)
            {
                ts = DateTime.Now - startTime;
                Thread.Sleep(20);
            }
            if (ts.TotalMilliseconds >= 10000)
            {
                ShowWarnAndPause($"治具[{testerId}]Socket[{bankNo}] StartTest超时", "系统");
                return false;
            }
            else
            {
                ResetStart(bankNo);
                if (!_startTest[bankNo])
                {
                    ShowWarnAndPause($"治具[{testerId}]Socket[{bankNo}]无法开始测试!请检查!", "系统");
                    return false;
                }
            }
            //TcpClientWait(_testerClient, sp, 10000, out string testReply);
            //if (!testReply.ToUpper().Contains("OK"))
            //{
            //    ShowWarnAndPause($"治具[{testerId}]Socket[{bankNo}]无法开始测试!请检查!", "系统");
            //    if (!CheckFlowCondition()) return;
            //}
            return true;
        }
        int count = 0;
        public bool AllEnabledTesterFinished(int bankIndex = 2)
        {
            if (GlobalVariable.IsDryRun || SystemConfig.Instance.UsingStatus.IsTesterSimulate)
            {
                Thread.Sleep(10);
                count++;
                if (count >= SystemConfig.Instance.TestParam.TestTimeCount)
                {
                    count = 0;
                    return true;
                }
                else
                    return false;
            }
            bool allEnabledTesterFinish = true;

            if (bankIndex == 2)
            {
                if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct &&
                    !TesterTray.Helper.GetCell(0).IsPreNG)
                {
                    if (!_resultReceivedFlag[0])
                        allEnabledTesterFinish = false;
                }
                if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct &&
                    !TesterTray.Helper.GetCell(1).IsPreNG)
                {
                    if (!_resultReceivedFlag[1])
                        allEnabledTesterFinish = false;
                }
                if (TesterTray.Helper.GetCell(0).IsPreNG)
                {
                    TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                }
                if (TesterTray.Helper.GetCell(1).IsPreNG)
                {
                    TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                }
            }
            else if(bankIndex == 0)
            {
                if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct &&
                    !TesterTray.Helper.GetCell(0).IsPreNG)
                {
                    if (!_resultReceivedFlag[0])
                        allEnabledTesterFinish = false;
                }
                if (TesterTray.Helper.GetCell(0).IsPreNG)
                {
                    TesterTray.Helper.SetCellResult(0, TestResult.FirstTimeNG);
                }
            }
            else if(bankIndex == 1)
            {
                if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct &&
                    !TesterTray.Helper.GetCell(1).IsPreNG)
                {
                    if (!_resultReceivedFlag[1])
                        allEnabledTesterFinish = false;
                }
                if (TesterTray.Helper.GetCell(1).IsPreNG)
                {
                    TesterTray.Helper.SetCellResult(1, TestResult.FirstTimeNG);
                }
            }

            return allEnabledTesterFinish;
        }
        public bool[] GetResult()
        {
            return _result;
        }
        public void ResetResult()
        {
            this._resultReceivedFlag[0] = false;
            this._resultReceivedFlag[1] = false;
        }
        public bool AllTesterEnabledStatusGot(int bankIndex = 2)
        {
            if (GlobalVariable.IsDryRun)
            {
                return true;
            }
            bool alreadyGot = true;

            if (bankIndex == 2)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_enableReceivedFlag[0])
                        alreadyGot = false;
                //}
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_enableReceivedFlag[1])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 0)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_enableReceivedFlag[0])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 1)
            {
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_enableReceivedFlag[1])
                        alreadyGot = false;
                //}
            }

            return alreadyGot;
        }
        public bool[] GetEanble()
        {
            return _enable;
        }
        public void ResetEnable(params int[] bankNo)
        {
            for (int i = 0; i < bankNo.Length; i++)
            {
                this._enableReceivedFlag[bankNo[i]] = false;
            }
        }
        public bool AllTesterReadyStatusGot(int bankIndex = 2)
        {
            if (GlobalVariable.IsDryRun)
            {
                return true;
            }
            bool alreadyGot = true;

            if (bankIndex == 2)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_readyReceivedFlag[0])
                        alreadyGot = false;
                //}
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_readyReceivedFlag[1])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 0)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_readyReceivedFlag[0])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 1)
            {
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_readyReceivedFlag[1])
                        alreadyGot = false;
                //}
            }

            return alreadyGot;
        }
        public bool[] GetReady()
        {
            return _ready;
        }
        public void ResetReady(params int[] bankNo)
        {
            for (int i = 0; i < bankNo.Length; i++)
            {
                this._readyReceivedFlag[bankNo[i]] = false;
            }
        }
        public bool AllTesterVacStatusGot(int bankIndex = 2)
        {
            if (GlobalVariable.IsDryRun)
            {
                return true;
            }
            bool alreadyGot = true;

            if (bankIndex == 2)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_vacReceivedFlag[0])
                        alreadyGot = false;
                //}
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_vacReceivedFlag[1])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 0)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_vacReceivedFlag[0])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 1)
            {
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_vacReceivedFlag[1])
                        alreadyGot = false;
                //
            }

            return alreadyGot;
        }
        public bool[] GetVac()
        {
            return _vac;
        }
        public void ResetVac(params int[] bankNo)
        {
            for (int i = 0; i < bankNo.Length; i++)
            {
                this._vacReceivedFlag[bankNo[i]] = false;
            }
        }
        public bool AllTesterStartStatusGot(int bankIndex = 2)
        {
            if (GlobalVariable.IsDryRun)
            {
                return true;
            }
            bool alreadyGot = true;

            if (bankIndex == 2)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_startTestReceivedFlag[0])
                        alreadyGot = false;
                //}
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_startTestReceivedFlag[1])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 0)
            {
                //if (TesterTray.Helper.GetCell(0).Status == CellStatus.HasProduct)
                //{
                    if (!_startTestReceivedFlag[0])
                        alreadyGot = false;
                //}
            }
            else if (bankIndex == 1)
            {
                //if (TesterTray.Helper.GetCell(1).Status == CellStatus.HasProduct)
                //{
                    if (!_startTestReceivedFlag[1])
                        alreadyGot = false;
                //}
            }

            return alreadyGot;
        }
        public bool[] GetStart()
        {
            return _startTest;
        }
        public void ResetStart(params int[] bankNo)
        {
            for (int i = 0; i < bankNo.Length; i++)
            {
                this._startTestReceivedFlag[bankNo[i]] = false;
            }
        }
    }
}
